package com.example.mrh.zyftpt_311mrh.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.mrh.zyftpt_311mrh.R;
import com.example.mrh.zyftpt_311mrh.bean.LoginBean;
import com.example.mrh.zyftpt_311mrh.iface.LoginListener;
import com.example.mrh.zyftpt_311mrh.model.LoginModel;

/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv1, tvname, tvwe, tvlogin, tv6;
    private EditText et1, et2;
    private Button btlogin, btreg;
    private ImageView imv;
    private Switch sw;
    private String username = "", password = "", sessionid = "";//用户名，密码
    private SharedPreferences sp;

    private LoginListener loginListener = new LoginListener() {
        @Override
        public void onResponse(LoginBean loginBean) {
            System.out.println("result:" + loginBean.getId());
            if (loginBean.getSessionid() != null) {

                sessionid = loginBean.getSessionid().toString();

                saveSP();
                //拿到sessionid之后再进行页面跳转
                Intent intent = new Intent(LoginActivity.this, MainActivity1.class);
                startActivity(intent);

            }
        }

        @Override
        public void onFail(String msg) {
            System.out.println("fail");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toast.makeText(this, "登录", Toast.LENGTH_SHORT).show();
        init();
        sp = getSharedPreferences("login", MODE_PRIVATE);
        //读取sp
        readSP();
    }

    //读取sp
    private void readSP() {
        String name = sp.getString("username", null);
        String pass = sp.getString("password", null);
        String session = sp.getString("sessionID", null);
        Boolean b = sp.getBoolean("remember", false);
        if (b) {
            et1.setText(name);
            et2.setText(pass);
            sw.setChecked(b);
        }
    }

    private void init() {
        //获取控件
        et1 = (EditText) findViewById(R.id.email);
        et2 = (EditText) findViewById(R.id.password);
        btlogin = (Button) findViewById(R.id.email_sign_in_button);
        btreg = (Button) findViewById(R.id.btn_reg);
        sw = (Switch) findViewById(R.id.switch1);
        //设置按钮监听器
        btlogin.setOnClickListener(this);
        btreg.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.email_sign_in_button:
                System.out.println("点击登陆按钮");
                username = et1.getText().toString();
                password = et2.getText().toString();
                if (username.equals(username) && password.equals(password)) {
                    saveSP();
                    LoginModel loginModel = new LoginModel();
                    loginModel.getResult(et1.getText().toString(), et2.getText().toString(), loginListener);
                }
                break;


            case R.id.btn_reg:
                // Intent intent1 = new Intent(LoginActivity.this, RegisterActivity.class);
                //startActivity(intent1);
                System.out.println("点击注册按钮");
                Intent intent = new Intent();
                intent.setClass(this, RegisterActivity.class);//forward为当前activity，forwardTarget为新的activity
                startActivity(intent);
                break;
        }
    }

    //保存sharedpreferences
    private void saveSP() {
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("username", username);
        editor.putString("password", password);
        editor.putString("sessionID", sessionid);
        editor.putBoolean("remember", sw.isChecked());
        editor.commit();//提交保存
    }

}
